import javax.swing.*;
import java.awt.event.*;

import javax.swing.border.TitledBorder;

import java.awt.GridLayout;
import java.awt.BorderLayout;

/**
 * EBook Organizer
 * 4/30/2010
 * <P>
 * The view to edit the different settings of the <code>EBookOrganizer</code>.
 * </P>
 * @version 0.1
 */
public class SettingsView extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3028688887063904339L;
	
	private SettingsModel model;
	
	private JTabbedPane jtpSettings = new JTabbedPane();
	private JTextField jtaFilePath = new JTextField("..\\saveFile.dat");
	private JButton jbtBrowseFilePath = new JButton("...");
	private JButton jbtSetFilePath = new JButton("set");
	
	private JComboBox jcbFileTypes = new JComboBox();
	private JTextField jtaType = new JTextField(".xxx", 4);
	private JTextField jtaProgramPath = new JTextField("C:\\...Program.exe", 20);
	private JButton jbtBrowse = new JButton("...");
	private JButton jbtAddFileType = new JButton("Add file type");
	private JButton jbtRemoveFileType = new JButton("Remove file type");
	private JCheckBox jchkEditDisk = new JCheckBox();
	
	public SettingsView(SettingsModel model) {
		this.setTitle("Settings");
		
		this.model = model;
		model.addActionListener( this );
		
		JPanel saveDataPanel = new JPanel();
		saveDataPanel.add( new JLabel("Location: ") );
		jtaFilePath.setEditable( false );
		saveDataPanel.add(jtaFilePath);
		jbtBrowseFilePath.setEnabled( false );
		saveDataPanel.add( jbtBrowseFilePath ) ;
		saveDataPanel.add(jbtSetFilePath);
		
		JPanel fileTypesPanelMain = new JPanel();
		fileTypesPanelMain.setLayout( new BorderLayout() );
		fileTypesPanelMain.add( jcbFileTypes, BorderLayout.NORTH );
		JPanel fileTypesPanel = new JPanel();
		fileTypesPanel.setBorder( new TitledBorder("") );
		fileTypesPanel.setLayout( new GridLayout(0, 3) );
		fileTypesPanel.add( new JLabel("FileType:") );
		fileTypesPanel.add( jtaType );
		fileTypesPanel.add( new JLabel("") );
		fileTypesPanel.add( new JLabel("ProgramPath:") );
		fileTypesPanel.add( jtaProgramPath );
		fileTypesPanel.add( jbtBrowse );
		fileTypesPanel.add( jbtAddFileType );
		fileTypesPanel.add( jbtRemoveFileType );
		
		JPanel checkPanel = new JPanel();
		checkPanel.add( new JLabel("Edit HDDrive") );
		jchkEditDisk.setEnabled( false );					/** Remove when coded */
		checkPanel.add( jchkEditDisk );
		
		fileTypesPanelMain.add(fileTypesPanel);
		fileTypesPanelMain.add(checkPanel, BorderLayout.SOUTH);
		
		jtpSettings.addTab("SaveData", saveDataPanel);
		jtpSettings.addTab("FileTypes", fileTypesPanelMain);
		add(jtpSettings);
		
		updateFileTypes();
		updateFileTypeFields();
		
		getFilePath().setText( model.getFilePath() );
		
		//Listener to update the fileType fields when a new filetype is selected
		jcbFileTypes.addItemListener( new ItemListener() {
				@Override
				public void itemStateChanged(ItemEvent arg0) {
					updateFileTypeFields();
				}
		});
	}
	
	private void updateFileTypes() {
		jcbFileTypes.removeAllItems();
		
		for (int i=0; i < model.getNumberOfFileTypes(); i++) {
			jcbFileTypes.addItem( model.getFileType( i ) );
		}
		
	}
	
	private void updateFileTypeFields() {
		FileType type = (FileType)jcbFileTypes.getSelectedItem();
		if (type != null) {
			jtaType.setText( type.getFileType() );
		jtaProgramPath.setText( type.getProgramPath() );
		}
	}
	
	public void setModel(SettingsModel m) {
		model = m;
		updateFileTypes();
	}
	
	public JButton getSetFilePathButton() {
		return jbtSetFilePath;
	}
	
	public JButton getSetFileButton() {
		return jbtSetFilePath;
	}
	
	public  JComboBox getFileTypes() {
		return jcbFileTypes;
	}
	
	public  JTextField getTypejta() {
		return jtaType;
	}
	
	public JTextField getProgramPath() {
		return jtaProgramPath;
	}
	
	public JButton getBrowseButton() {
		return jbtBrowse;
	}
	
	public JButton getAddFileTypeButton() {
		return jbtAddFileType;
	}
	
	public JButton getRemoveFileTypeButton() {
		return jbtRemoveFileType;
	}
	
	public JTextField getFilePath() {
		return jtaFilePath;
	}
	
	public JButton getFileBrowseButton() {
		return jbtBrowseFilePath;
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		updateFileTypes();
	}

}
